package com.itheima.reggie.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.itheima.reggie.common.R;
import com.itheima.reggie.dto.OrdersDto;
import com.itheima.reggie.pojo.Category;
import com.itheima.reggie.pojo.OrderDetail;
import com.itheima.reggie.pojo.Orders;
import com.itheima.reggie.pojo.ShoppingCart;
import com.itheima.reggie.service.OrderDetailService;
import com.itheima.reggie.service.OrdersService;
import com.itheima.reggie.service.ShoppingCartService;
import com.mysql.cj.log.Log;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.time.LocalDateTime;
import java.util.Arrays;
import java.util.List;

/**
 * 订单管理
 */
@Slf4j
@RestController
@RequestMapping("order")
public class OrdersController {

    @Autowired
    private OrdersService ordersService;
    @Autowired
    OrderDetailService orderDetailService;
    @Autowired
    ShoppingCartService shoppingCartService;

    /**
     * 用户下单
     */
    @PostMapping("submit")
    public R<String> submit(@RequestBody Orders orders) {
        ordersService.submit(orders);
        return R.success("下单成功");
    }

    /**
     * 用户订单
     */
    @GetMapping("userPage")
    public R<Page<Orders>> userOrderPage(Integer page, Integer pageSize) {
        Page<Orders> ordersPage = ordersService.userOrderPage(page, pageSize);
        return R.success(ordersPage);
    }

    /**
     * 查询订单信息
     */
    @GetMapping("page")
    public R<Page<Orders>> page(Integer page, Integer pageSize, Long number, String beginTime, String endTime){
        Page<Orders> pageInfo = new Page<>(page, pageSize);
        ordersService.query()
                .eq(number != null, "number", number)
                .between(beginTime != null && endTime != null, "order_time", beginTime, endTime)
                .page(pageInfo);
        return R.success(pageInfo);
    }

    @PostMapping("again")
    public R<List<OrderDetail>> again(Long id) {
        List<OrderDetail> list = orderDetailService.query()
                .eq("order_id", id)
                .list();

        return R.success(list);
    }
}
